package com.hejun.websocket.controller;

import com.hejun.websocket.doMian.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class MessageController {
    @Autowired
    private SimpMessagingTemplate messagingTemplate;

    @GetMapping ("/")
    public String toChatAll(){
        return "chatAll";
    }

    @MessageMapping("/sendAll") //客户端发送消息的路径如果是/app/sendAll,将交由此方法进行处理
    public void sendAll(Message message) { //接收到客户端发送过来的消息
        //发送消息目标地址为/topic/receiveAll，所有订阅了/topic/receiveAll的客户端将收到此消息（广播）
        // 以/topic为前缀的代表广播
        messagingTemplate.convertAndSend("/topic/receiveAll", message);
    }

}
